package com.common.common.option;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 分页查询参数
 * @param <T> 返回数据类型
 *
 * @author liujun
 * @date 2024/10/10
 */
@Data
@NoArgsConstructor
public class QueryPageOption<T> {
    private int code;
    private String msg;
    private List<T> data;
    private PageOption pagination;
    private OrderOption orderBy;
    private QueryCondition query;

    public QueryPageOption(int code, String msg, List<T> data, PageOption pagination, OrderOption orderBy, QueryCondition query) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.pagination = pagination;
        this.orderBy = orderBy;
        this.query = query;
    }

    public QueryPageOption(int code, String msg, List<T> data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Page<T> toPage() {
        if (pagination == null) {
            pagination = new PageOption(1L, 10L, null);
        } else {
            if (pagination.getCurrentPage() == null) {
                pagination.setCurrentPage(1L);
            }
            if (pagination.getPageSize() == null) {
                pagination.setPageSize(10L);
            }
        }
        Page<T> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
        page.setCountId("id");
        page.setOptimizeCountSql(false);
        return page;
    }

    public QueryWrapper<T> toQueryWrapper() {
        if (query == null) {
            query = new QueryCondition();
        }
        QueryWrapper<T> queryWrapper = query.toQueryWrapper();
        if (needOrderBy()) {
            queryWrapper.orderBy(true, !"descending".equals(orderBy.getOrder()), orderBy.getProp());
        }
        return queryWrapper;
    }

    private boolean needOrderBy() {
        return orderBy != null && orderBy.getProp() != null && !orderBy.getProp().isEmpty();
    }

    public QueryPageOption<T> success() {
        this.code = 200;
        this.msg = "操作成功";
        return this;
    }

    public QueryPageOption<T> success(String msg) {
        this.code = 200;
        this.msg = msg;
        return this;
    }

    public QueryPageOption<T> success(List<T> rows) {
        return this.success().setData(rows);
    }

    public QueryPageOption<T> success(String msg, List<T> rows) {
        return this.success(msg).setData(rows);
    }

    public QueryPageOption<T> error() {
        this.code = 500;
        this.msg = "操作失败";
        return this;
    }

    public QueryPageOption<T> error(String msg) {
        this.code = 500;
        this.msg = msg;
        return this;
    }

    public QueryPageOption<T> warn() {
        this.code = 400;
        this.msg = "操作有误";
        return this;
    }

    public QueryPageOption<T> warn(String msg) {
        this.code = 400;
        this.msg = msg;
        return this;
    }

    public QueryPageOption<T> setPagination(PageOption pagination) {
        this.pagination = pagination;
        return this;
    }

    public QueryPageOption<T> setOrderBy(OrderOption orderBy) {
        this.orderBy = orderBy;
        return this;
    }

    public QueryPageOption<T> setQuery(QueryCondition query) {
        this.query = query;
        return this;
    }

    public QueryPageOption<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public QueryPageOption<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public QueryPageOption<T> setData(List<T> data) {
        this.data = data;
        return this;
    }

    public boolean isSuccess() {
        return code == 200;
    }

    public boolean isWarn() {
        return code == 400;
    }

    public boolean isError() {
        return code == 500;
    }
}
